package com.travel.manager.controller;

import com.github.pagehelper.PageInfo;
import com.travel.common.pojo.City;
import com.travel.common.pojo.Hotel;
import com.travel.common.util.JsonUtil;
import com.travel.index.service.HotelIndexService;
import com.travel.manager.service.CityService;
import com.travel.manager.service.HotelService;
import com.travel.manager.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hotel")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    @Autowired
    private CityService cityService;

    /**
     * 去列表页
     *
     * @param model
     * @return
     */
    @RequestMapping("/go_list.html")
    public String goHotelList(Model model) {
        //查询出城市列表，不分页
        List<City> cityList = cityService.getAllCityList();
        model.addAttribute("cityList", cityList);
        return "/hotel_list.jsp";
    }

    /**
     * 城市列表页，页面使用easyUI的datagrid
     *
     * @param hotel 查询条件
     * @param page  页码，参数名不能变
     * @param rows  每页几条，参数名不能变
     * @return
     */
    @RequestMapping("/list.html")
    @ResponseBody
    public Map<String, Object> list(Hotel hotel, Integer page, Integer rows) {
        PageInfo<Hotel> pageInfo = hotelService.getHotelList(hotel, page, rows);
        Map<String, Object> result = new HashMap<>();
        //rows是datagrid需要的数据列表，这个key不能变
        result.put("rows", pageInfo.getList());
        //total是datagrid需要的数据总条数，这个key不能变
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @RequestMapping("/go_edit.html")
    public String goEdit(Model model, @RequestParam(required = false) Integer id) {
        //城市列表
        List<City> cityList = cityService.getAllCityList();
        model.addAttribute("cityList", cityList);
        if (id != null) {//有id表示修改。查询出数据传到页面上
            Hotel hotel = hotelService.getHotelById(id);
            model.addAttribute("hotel", hotel);
        }
        return "/hotel_edit.jsp";
    }

    @RequestMapping("/edit.html")
    @ResponseBody
    public Map<String, Object> editHotel(Hotel hotel) {
        if (hotel.getId() == null) {//如果没有id就是添加
            hotelService.addHotel(hotel);
        } else {//有id就是修改
            hotelService.updateHotel(hotel);
        }
        return JsonUtil.getOkMsg("编辑成功", null);
    }


    @Value("${IMAGE_SERVER_URL}")
    private String IMAGE_SERVER_URL;

    @RequestMapping("/upload.html")
    @ResponseBody
    public String picUpload(MultipartFile file) throws Exception {
        try {
            // 接收上传的文件
            // 取扩展名
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            // 上传到图片服务器
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf");
            String url = fastDFSClient.uploadFile(file.getBytes(), extName);
            url = IMAGE_SERVER_URL + url;
            // 响应上传图片的url
            Map result = new HashMap<>();
            result.put("error", 0);
            result.put("url", url);
            return JsonUtil.getJSON(result);// 必须返回字符串，如果是Object,浏览器兼容有问题
        } catch (Exception e) {
            e.printStackTrace();
            Map result = new HashMap<>();
            result.put("error", 1);
            result.put("message", "图片上传失败");
            return JsonUtil.getJSON(result);
        }

    }

    @Autowired
    private HotelIndexService hotelIndexService;

    @RequestMapping("/solr.html")
    @ResponseBody
    public Map<String, Object> updateSolr(Integer hotelId) {
        boolean result = hotelIndexService.doUpdateHotelIndex(hotelId);
        if (result) {
            return JsonUtil.getOkMsg("更新成功", null);
        } else {
            return JsonUtil.getErrorMsg("更新失败");
        }
    }

}
